﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data.SqlClient;

namespace TinyCinema.Data
{

    public class SchedulePrice : Execution
    {
        public string ID { get; set; }
        public TimeSpan StartTime { get; set; }
        public TimeSpan EndTime { get; set; }
        public DayOfWeek Day { get; set; }
        public Decimal Price { get; set; }
        public ScheduleType ScheduleType { get; set; }

        public ObservableCollection<Schedule> Schedules { get; set; }
        public SchedulePrice(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.ScheduleTypePrice);
            var tbl = ds.Tables[Tables.ScheduleTypePrice];
            var Row = tbl.Select(SchedulePriceTable.ID + "='" + ID + "'")[0];
            StartTime = (TimeSpan)Row[SchedulePriceTable.StartTime];
            EndTime = (TimeSpan)Row[SchedulePriceTable.EndTime];
            Day = (DayOfWeek)Row[SchedulePriceTable.Day];
            try { Price = (Decimal)Row[SchedulePriceTable.Price]; }
            catch { }
            ScheduleType = new ScheduleType(Row[SchedulePriceTable.SchedulePriceTypeID] as String);
        }
        public SchedulePrice()
        {
            ID = IDGenerate(Tables.ScheduleTypePrice);
            ScheduleType = new ScheduleType();
        }
        public static ObservableCollection<SchedulePrice> GetAllSchedulePrice()
        {
            PutTableIntoDataSet(Tables.ScheduleTypePrice);
            var Result = new ObservableCollection<SchedulePrice>();
            var tbl = ds.Tables[Tables.ScheduleTypePrice];
            var Rows = tbl.Select();
            foreach (var Row in Rows)
                Result.Add(new SchedulePrice(Row[SchedulePriceTable.ID] as String));
            return Result;
        }
        public void Save()
        {
            var tbl = ds.Tables[Tables.ScheduleTypePrice];
            var Rows = tbl.Select(SchedulePriceTable.ID + "='" + ID + "'");
            if (Rows.Length==0) //Đơn giá đang lưu là đơn giá được tạo mới
            {
                var Row = tbl.NewRow();
                Row[SchedulePriceTable.ID] = ID;
                Row[SchedulePriceTable.Day] = Day;
                Row[SchedulePriceTable.StartTime] = StartTime;
                Row[SchedulePriceTable.EndTime] = EndTime;
                Row[SchedulePriceTable.SchedulePriceTypeID] = ScheduleType.ID;
                Row[SchedulePriceTable.Price] = Price;
                tbl.Rows.Add(Row);
            }
            else
            {
                var Row = Rows[0];
                Row[SchedulePriceTable.Day] = Day;
                Row[SchedulePriceTable.StartTime] = StartTime;
                Row[SchedulePriceTable.EndTime] = EndTime;
                Row[SchedulePriceTable.SchedulePriceTypeID] = ScheduleType.ID;
                Row[SchedulePriceTable.Price] = Price;
            }
            SaveCommand(Tables.ScheduleTypePrice);            
        }
        public void Delete()
        {
            var tbl = ds.Tables[Tables.ScheduleTypePrice];
            var Row = tbl.Select(SchedulePriceTable.ID + "='" + ID + "'")[0];
            Row.Delete();
            SaveCommand(Tables.ScheduleTypePrice);
        }

    }
}

        
